// /Users/bytedance/Desktop/video-template/miniprogram-templates/templates/microapp/javascript/video-template/components/error-page/error-page.js
const _api = require('../../utils/api.js');
const apiurl = require('../../utils/apiurl/public.js');
Component({
  data: {
    code:'',
    getuser:'',
    showPay:false,
    type:1
  },
  properties: {
    showPay:{
      type:Boolean,
      value:false
    },
    type:{
      type:Number,
      value:1
    },
  }, pageLifetimes: {
     show: async function () { 
      console.log('getuser',tt.getStorageSync('getuser'))
      if(tt.getStorageSync('getuser')){
        
        this.setData({
          showPay: true
        })
      }
      let code = await _api.ttlogin();
      // 页面被展示
      this.showFavoriteGuide()
    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function () {
      // 页面尺寸变化
    },
  },
  methods: {
    async showFavoriteGuide() {
      let _this=this;
      let code = await _api.ttlogin();
      this.setData({
        code: code
      })
      _this.login()
    
    },
    login(){
      tt.getUserProfile({
        success: async ures => {

         console.log(this.data.code)
          let objdata = {
            'js_code': this.data.code,
            'encryptedData': ures.encryptedData,
            'iv': ures.iv,
            'type': 'tt'
          };
          _api.post(apiurl.login, objdata).then(async loginres => {

            if(loginres.code){
             let code = await _api.ttlogin();
             this.setData({
              code: code
            })
            }
            tt.setStorageSync('token', loginres.data.data.token)
            let userinfo = await _api.getUserinfo()
            this.setData({
              getuser: userinfo
            })
            if(userinfo){
              this.setData({
                showPay: true
              })
              let params=tt.getStorageSync('source');
              console.log('检测是否有路口文件',params)
              if(tt.getStorageSync('source')){
                _api.post('index/user/dd_data_report',params).then(qdres=>{
                  tt.setStorageSync('source','')
                })
              }
            }
            console.log(userinfo)
          })

        },
        fail: err => {
          console.log(err)

        }
      })
    }
  }
})